//
//  HLMainController.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/14.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

class HLMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //利用KVC设置自定义tabbar
        setValue(HLMainTabBar(), forKey: "tabBar")
        //添加子控制器
        addChildViewcontrollers()
    }

    private func addChildViewcontrollers () {
        //创建控制器
        let homeVC = HLHomeController()
        let messageVC = HLMessageController()
        let discoverVC = HLDiscoverController()
        let profileVC = HLProfileController()
        //添加
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    //抽取添加子控制器方法
    private func addChildViewController(viewController: HLBaseController, title: String, imageName: String) {
        //设置导航控制器
        let nav = UINavigationController(rootViewController: viewController)
        //设置文字
        viewController.title = title
        //设置选中状态下文字颜色
        viewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //设置图片
        viewController.tabBarItem.image = UIImage(named: imageName)
        //设置选中状态下图片,并且使图片不渲染
        viewController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(.AlwaysOriginal)
        //添加到父控制器中
        addChildViewController(nav)
    }
    

}
